<template>
  <layout>
    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        :FormList="searchColumns"
      >
        <a-button
          class="action-button"
          type="primary"
          @click="search()"
        >搜索</a-button>
        <a-button
          class="action-button"
          type="primary"
          auth="power.add"
          @click="tableAdd"
        >添加</a-button>
      </custom-form>
      <!-- <a-input placeholder="产品名称或者编号" v-model="tableSearch.searchKey" allowClear style="width: 200px;" /> -->
    </section>

    <!-- 产品列表 -->
    <a-table
      :columns="tableColumns"
      :dataSource="tableData"
      :rowKey="record => record.power_id"
      :customRow="customTableRow"
      :pagination="false"
      bordered
    >
      <template
        slot="is_menu"
        slot-scope="row"
      >
        {{getLabel(row.is_menu, [], 'is_menu')}}
      </template>
      <!-- 按钮区 -->
      <template
        slot="action"
        slot-scope="row"
      >
        <a-button
          class="action-button"
          type="link"
          auth="power.edit"
          @click="handleTableEdit(row)"
        >编辑</a-button>
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button
            class="action-button"
            type="link"
            auth="power.delete"
          >删除</a-button>
        </a-popconfirm>
        <a-button
          class="action-button"
          type="link"
          auth="power.next"
          v-if="+row.is_menu === 1"
          @click="handlerAddNext(row)"
        >添加下级</a-button>
        <a-button
          class="action-button"
          type="link"
          auth="power.button"
          v-if="+row.is_menu === 1 && !row.children"
          @click="handlerButtonPower(row)"
        >按钮权限</a-button>
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <!-- <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination> -->

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      @cancel="visible = false"
      @ok="handleSubmit"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="formMode"
        v-bind="formItemLayout"
        layout="horizontal"
      ></custom-form>
      <!-- <section slot="footer"> -->
      <!-- <a-button
        @click="handleSubmit"
        type="primary"
      >提交</a-button> -->
      <!-- </section> -->
    </a-modal>

    <a-drawer
      :title="currentRow.power_nam + '-按钮权限'"
      placement="right"
      :closable="false"
      :width="800"
      :visible="drawerVisible"
      @close="drawerVisible = false"
    >
      <ButtonList :currentPower="currentRow" />
    </a-drawer>

  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { poserServices, IPower, IPowerGroup } from '@/common/services/power'
import { MenuTypeEnum } from '@/common/enum'
import ButtonList from './ButtonList.vue'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm,
    ButtonList
  }
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  // 产品列表搜索条件
  public tableSearch = {
    pow_gp_nam: ''
  }

  // 公共参数
  public commonParams = {
    is_menu: '1'
  }

  // 打开抽屉
  public drawerVisible = false

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '菜单编号',
      dataIndex: 'power_id',
      key: 'power_id',
      form: {
        type: 'a-input',
        placeholder: '编号自动生成',
        disabled: true,
        isShowList: false
      }
    },
    {
      title: '上级菜单',
      dataIndex: 'power_up_id',
      key: 'power_up_id',
      form: {
        type: 'a-tree-select',
        placeholder: '',
        isShowList: false,
        treeData: [],
        treeDefaultExpandAll: true,
        replaceFields: {
          title: 'power_nam',
          key: 'power_id',
          value: 'power_id'
        }
      }
    },
    {
      title: '菜单名称',
      dataIndex: 'power_nam',
      key: 'power_nam',
      form: {
        type: 'a-input',
        placeholder: '请输入菜单名称',
        isShowQuery: true
      }
    },
    // {
    //   title: '类型',
    //   key: 'is_menu',
    //   scopedSlots: { customRender: 'is_menu' },
    //   form: {
    //     type: 'a-radio-group',
    //     placeholder: '请输入',
    //     defaultValue: '1',
    //     dict: [
    //       {
    //         value: '1',
    //         label: '菜单'
    //       },
    //       {
    //         value: '2',
    //         label: '按钮'
    //       }
    //     ]
    //   }
    // },
    {
      title: '访问地址',
      dataIndex: 'power_web_url',
      key: 'power_web_url',
      form: {
        type: 'a-input',
        placeholder: '请输入访问URL地址'
      }
    },
    {
      title: '排序值',
      dataIndex: 'power_index',
      key: 'power_index',
      form: {
        type: 'a-input',
        placeholder: '请输入排序值'
      }
    },
    {
      title: '操作',
      key: 'action',
      width: 240,
      scopedSlots: { customRender: 'action' }
    }
  ]

  public tableApi = {
    list: () => poserServices.getPowerList([MenuTypeEnum.MENU]),
    add: poserServices.updatePower,
    edit: poserServices.updatePower,
    del: poserServices.deletePower,
    imageUpload: 'uploadStoreImage',
    imageRemove: 'delStoreImage'
  }

  public mounted() {
    // 修改索引列默认宽度
    if (this.tableColumns[0] && this.tableColumns[0].key === 'index') {
      this.tableColumns[0].width = 60
    }
    this.getData()
  }

  public async getData() {
    const res = await this.getTableList()
    this.tableData = res.data || []
    this.$nextTick(() => {
      this.changeDict('power_up_id', this.tableData, 'treeData')
    })
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: any) {
    this.tableEdit(Object.assign({}, row))
  }

  public handlerAddNext(row: IPower) {
    this.tableAdd()
    this.$nextTick(() => {
      this.currentRow = Object.assign(
        {},
        { power_up_id: row.power_id }
      )
    })
  }

  public handlerButtonPower(row: IPower) {
    this.currentRow = Object.assign({}, row)
    this.drawerVisible = true
  }
}
</script>

<style scoped>
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
</style>
